package com.mls.guardian.common;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;

import static java.nio.file.Files.copy;
import static java.nio.file.Files.delete;
import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Paths.get;

/**
 * Created by jl on 15/10/21.
 */
public class FileUtil {
    public static String file2Str(String filePath) throws IOException {
        return new String(readAllBytes(get(filePath)));
    }

    /**
     * 复制文件夹
     *
     * @param sourceFolder d:\test
     * @param targetFolder e:\test
     * @param filter       .+.js$ 复制文件过滤正则
     * @param isForce      是否强制复制
     * @throws IOException
     */
    public static void copyFolder(String sourceFolder, String targetFolder, String filter, boolean isForce) throws IOException {
        if (Files.notExists(Paths.get(targetFolder))) {
            Files.createDirectories(Paths.get(targetFolder));
        }

        File folder = new File(sourceFolder);
        File[] files = folder.listFiles();
        for (File item : files) {
            if (item.isDirectory()) {
                copyFolder(item.getPath(), targetFolder + File.separator + item.getName(), isForce);
            } else if (item.isFile()) {
                Path sourcePath = Paths.get(sourceFolder, item.getName());
                Path sourceTarget = Paths.get(targetFolder, item.getName());

                //正则匹配
                if (sourcePath.toString().matches(filter)) {
                    if (true == isForce) {
                        Files.copy(sourcePath, sourceTarget, StandardCopyOption.REPLACE_EXISTING);
                    } else {
                        Files.copy(sourcePath, sourceTarget);
                    }
                }
            }
        }
    }

    /**
     * 复制文件夹
     *
     * @param sourceFolder d:\test
     * @param targetFolder e:\test
     * @param isForce      是否强制复制
     * @throws IOException
     */
    public static void copyFolder(String sourceFolder, String targetFolder, boolean isForce) throws IOException {
        if (Files.notExists(Paths.get(targetFolder))) {
            Files.createDirectories(Paths.get(targetFolder));
        }

        File folder = new File(sourceFolder);
        File[] files = folder.listFiles();
        for (File item : files) {
            if (item.isDirectory()) {
                copyFolder(item.getPath(), targetFolder + File.separator + item.getName(), isForce);
            } else if (item.isFile()) {
                Path sourcePath = Paths.get(sourceFolder, item.getName());
                Path sourceTarget = Paths.get(targetFolder, item.getName());
                if (true == isForce) {
                    Files.copy(sourcePath, sourceTarget, StandardCopyOption.REPLACE_EXISTING);
                } else {
                    Files.copy(sourcePath, sourceTarget);
                }
            }
        }
    }

    /**
     * 移动文件夹
     *
     * @param sourceFolder d:\test
     * @param targetFolder e:\test
     * @throws IOException
     */
    public static void moveFolder(String sourceFolder, String targetFolder) throws IOException {
        if (Files.notExists(Paths.get(targetFolder))) {
            Files.createDirectories(Paths.get(targetFolder));
        }

        File folder = new File(sourceFolder);
        File[] files = folder.listFiles();
        for (File item : files) {
            if (item.isDirectory()) {
                moveFolder(item.getPath(), targetFolder + File.separator + item.getName());
            } else if (item.isFile()) {
                Path sourcePath = Paths.get(sourceFolder, item.getName());
                Path sourceTarget = Paths.get(targetFolder, item.getName());

                Files.move(sourcePath, sourceTarget, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }

    /**
     * 删除目录下面所有文件
     *
     * @param folderName
     */
    public static void deleteFolder(String folderName) {
        File file = new File(folderName);
        if (file.isDirectory()) {
            for (File item : file.listFiles()) {
                deleteFolder(item.getPath());
            }
            //没有文件了删除目录
            if (file.listFiles() == null || file.listFiles().length == 0) {
                file.delete();
            }
        } else if (file.isFile()) {
            file.delete();
        }
    }

    /**
     * 删除目录下面制定条件文件
     *
     * @param folderName
     * @param filter
     */
    public static void deleteFolder(String folderName, String filter) {
        File file = new File(folderName);
        if (file.isDirectory()) {
            for (File item : file.listFiles()) {
                deleteFolder(item.getPath(), filter);
            }
            //没有文件了删除目录
            if (file.listFiles() == null || file.listFiles().length == 0) {
                file.delete();
            }
        } else if (file.isFile() && file.getName().matches(filter)) {
            file.delete();
        }
    }

    /**
     * 向指定文件追加内容
     *
     * @param filePath 文件路径
     * @param message  内容
     * @throws Exception
     */
    public static void append(String filePath, String message) throws Exception {
        if (Files.notExists(Paths.get(filePath).getParent())) {
            Files.createDirectories(Paths.get(filePath).getParent());
        }
        if (Files.notExists(Paths.get(filePath))) {
            Files.createFile(Paths.get(filePath));
        }
        if (message != null && message.trim().length() > 0) {
            Files.write(Paths.get(filePath), (message.trim() + System.getProperty("line.separator")).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
        }
    }

    /**
     * 向指定文件写入内容
     *
     * @param filePath 文件路径
     * @param message  内容
     * @throws Exception
     */
    public static void write(String filePath, String message) throws Exception {
        write(filePath, message, StandardOpenOption.WRITE);
    }


    public static void write(String filePath, String message, OpenOption openOption) throws Exception {
        if (openOption == null) {
            openOption = StandardOpenOption.WRITE;
        }
        if (Files.notExists(Paths.get(filePath).getParent())) {
            Files.createDirectories(Paths.get(filePath).getParent());
        }
        if (Files.notExists(Paths.get(filePath))) {
            Files.createFile(Paths.get(filePath));
        }
        if (message != null && message.trim().length() > 0) {
            Files.write(Paths.get(filePath), (message.trim() + System.getProperty("line.separator")).getBytes(StandardCharsets.UTF_8), openOption);
        }
    }

}
